<template>
  <!-- <templateModal :modalOption="modalOption" title="导入" @modalCloseHandle="addUserModalClose">
    <template #modalContent>
      <div class="import-template">
        导入模板：
        <a-button ghost type="primary" @click="download"> 下载</a-button>
      </div>
      <div class="upload-result" v-if="uploadSuccess">
        共上传{{ importNum }}条，成功{{ successUploadData }}条，
        <span style="color: red">失败{{ failUploadData }}条</span>
        <span style="color: #1890ff; cursor: pointer" @click="downloadFile"> 下载文件</span>
      </div>
    </template>
<template #footer>
      <div class="upload-footer">
        <a-upload v-modal:file-list="fileList" name="file" :data="uploadData.data" :action="upLoadUrl"
          :headers="{ Authorization: token }" @change="handleChange" :before-upload="beforeUpload">
          <a-button type="primary"> 上传 </a-button>
        </a-upload>
        <a-button type="addUserModalClose" class="cancel-btn" @click="addUserModalClose"> 取消 </a-button>
      </div>
    </template>
</templateModal> -->
  <upload-excel :on-success="success" />
</template>
<script>
export default {
  methods: {
    async success({ header, results }) {
      console.log(header)
      console.log(results)
      const userRelations = {
        '日期': 'theDate',
        '手机': 'mobile',
        '姓名': 'userName',
        '工号': 'workNum'
      }

      let newArr = results.map(item => {
        const userInfo = {}
        Object.keys(item).forEach(k => {
          if (userRelations[k] === 'timeOfEntry' || userRelations[k] === 'correctionTime') {
            userInfo[userRelations[k]] = new Date(this.formatDate(item[k], '/'))
            return
          }
          userInfo[userRelations[k]] = item[k]
        })
        return userInfo
      })
      await importEmployee(newArr)
      this.$message.success('导入成功')
      this.$router.back()
    },
    formatDate(numb, format) {
      const time = new Date((numb - 1) * 24 * 3600000 + 1)
      time.setFullYear(time.getFullYear() - 70)
      const year = time.getFullYear() + ''
      const month = time.getMonth() + 1 + ''
      const date = time.getDate() - 1 + ''
      if (format && format.length === 1) {
        return year + format + month + format + date
      }
      return year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date)
    },

    exportData() {
      const headers = {
        '姓名': 'userName',
        '手机号': 'mobile',
        '入职日期': 'timeOfEntry',
        '聘用形式': 'formOfEmployment',
        '转正日期': 'correctionTime',
        '工号': 'workNumber',
        '部门': 'departmentName'
      }

      import('@/vendor/Export2Excel').then(async excel => {
        const { rows } = await getEmployeeList({ page: 1, size: this.page.total })
        const data = this.formatJson(headers, rows)
        excel.export_json_to_excel({
          header: Object.keys(headers),
          data,
          fileName: 'excel-list',
          autoWidth: true,
          bookType: 'xlsx'
        })
      })
    },
    formatJson(headers, rows) {
      return rows.map(item => {
        console.log(item)
        return Object.keys(headers).map(k => {
          if (headers[k] === 'timeOfEntry' || headers[k] === 'correctionTime') {
            return this.formatDate(item[headers[k]])
          } else if (headers[k] === 'formOfEmployment') {
            return EmployeeEnum.hireType[item.formOfEmployment] ? EmployeeEnum.hireType[item.formOfEmployment] : '未知'
          }
          return item[headers[k]]
        })
      })
    }
  }
}//https://devpress.csdn.net/vue/66cc41a77648734c1b17259b.html?dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NjQ1Mjg0MiwiZXhwIjoxNzM5NDExODMxLCJpYXQiOjE3Mzg4MDcwMzEsInVzZXJuYW1lIjoid2VpeGluXzQ1ODAyMDg4In0.4EgOMo-wFgHnNSjNBKTgBD723f74CHM03wzPlDc52zQ&spm=1001.2101.3001.6650.2&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EOPENSEARCH%7Eactivity-2-117560118-blog-134552013.235%5Ev43%5Epc_blog_bottom_relevance_base7&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EOPENSEARCH%7Eactivity-2-117560118-blog-134552013.235%5Ev43%5Epc_blog_bottom_relevance_base7&utm_relevant_index=5
</script>
<style lang="">

</style>